How to disable text selection highlighting
Asked Answered
V

45

6093

For anchors that act like buttons (for example, the buttons on the sidebar of this Stack Overflow page titled Questions, Tags, and Users) or tabs, is there a CSS standard way to disable the highlighting effect if the user accidentally selects the text?

I realize that this could be done with JavaScript and a little googling yielded the Mozilla-only -moz-user-select option.

Is there a standard-compliant way to accomplish this with CSS, and if not, what is the "best practice" approach?

Visor answered 5/5, 2009 at 20:29 Comment(7)
can elements within the element witch has highlighting disabled, have highlighting enabled with in css in the style or class attribute? or in other words, are there other values for -webkit-user-select ect. other than just none?Weka
Related: #16600979 = how to allow only some of the child elements to be selectedAngelikaangelina
There a bug in some browsers where doing "Select All" (CTRL+A and CMD+A) still selects things. This can be fought with a transparent selection color: ::selection { background: transparent; } ::-moz-selection { background: transparent; }Moppet
In year 2017, it is better way to use postcss and autoprefixer and set browser version, then postcss make everything cool.Landfall
The user interface changed. In 2019, all three mentioned items are now in a hamburger menu in the upper left. "Tags" and "Users" are in there, and "Questions" is now called "Stack Overflow" (with an icon in front).Wandawander
pointer-events: none; worked for me in iframeSupertax
Please never do this on actual text (I understand buttons or other small elements might need this). Highlighting text as you read is an important coping strategy for dyslexia, and disabling it makes your webpage harder to read for those individuals.Marmoset
J
8568

UPDATE January, 2017:

According to Can I use, the user-select + -webkit-user-select for Safari is enough to achieve desired behavior in all major browsers.


These are all of the available correct CSS variations:

.noselect {
  -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Old versions of Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome, Edge, Opera and Firefox */
}
<p>
  Selectable text.
</p>
<p class="noselect">
  Unselectable text.
</p>

Note that user-select is in standardization process (currently in a W3C working draft). It is not guaranteed to work everywhere and there might be differences in implementation among browsers. Also, browsers can drop support for it in the future.


More information can be found in Mozilla Developer Network documentation.

The values of this attribute are none, text, toggle, element, elements, all and inherit.

Jacksnipe answered 5/5, 2009 at 20:29 Comment(9)
nice code molokoloco :D , although I personally would stay well away from using it, as sometimes you may need the values different for different browsers, and it relys on JavaScript. Making a class and adding it to your element or applying the css to your type of element in your style-sheet is pretty bullet proof.Jacksnipe
'user-select'- Values: none | text | toggle | element | elements | all | inherit - w3.org/TR/2000/WD-css3-userint-20000216Jacksnipe
this is ridiculous! so many different ways to do the same thing. let's make a new standard for user selects. we will call it standard-user-select. then we won't have these problems. although for backwards compatibility we should include the others as well. so now the code becomes -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; standard-user-select: none;. ah, much better.Olympia
According to caniuse it seems that it doesn't need those prefixes anymore.Prog
@Prog In that case, caniuse.com is wrong. I still need the -webkit-user-select: none; line using Safari on iOS 15.1.Wingfooted
Instead of putting all that in a class I add it to html, body { ... here... } to apply it globally. Doesn't seem to be causing any problems so far.Patch
-webkit-user-select: none; also disables macOS Live Text. To get rid of the text cursor though, you will need an additional cursor: default.Ferroelectric
Actually user-select doesn’t work on Safari, you still have to use the -webkit prefix. See the little yellow sticky on the green cells on CanIuseCranio
September 2023: Safari still need the -webkit- prefix. What is worse: .style.webkitUserSelect = 'none' is deprecated and sets user-select: none. One workaround is to use .setAttribute("style", "-webkit-user-select: none; -webkit-touch-callout: none;")Mountainside
A
956

In most browsers, this can be achieved using proprietary variations on the CSS user-select property, originally proposed and then abandoned in CSS 3 and now proposed in CSS UI Level 4:

*.unselectable {
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in Internet Explorer 10.
     See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}

For Internet Explorer < 10 and Opera < 15, you will need to use the unselectable attribute of the element you wish to be unselectable. You can set this using an attribute in HTML:

<div id="foo" unselectable="on" class="unselectable">...</div>

Sadly this property isn't inherited, meaning you have to put an attribute in the start tag of every element inside the <div>. If this is a problem, you could instead use JavaScript to do this recursively for an element's descendants:

function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.setAttribute("unselectable", "on");
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));

Update 30 April 2014: This tree traversal needs to be rerun whenever a new element is added to the tree, but it seems from a comment by @Han that it is possible to avoid this by adding a mousedown event handler that sets unselectable on the target of the event. See http://jsbin.com/yagekiji/1 for details.


This still doesn't cover all possibilities. While it is impossible to initiate selections in unselectable elements, in some browsers (Internet Explorer and Firefox, for example) it's still impossible to prevent selections that start before and end after the unselectable element without making the whole document unselectable.

Anorthite answered 5/12, 2010 at 11:45 Comment(1)
Instead of using the class="unselectable", just use the attribute selector [unselectable="on"] { … }Superdreadnought
D
234

Until CSS 3's user-select property becomes available, Gecko-based browsers support the -moz-user-select property you already found. WebKit and Blink-based browsers support the -webkit-user-select property.

This of course is not supported in browsers that do not use the Gecko rendering engine.

There is no "standards" compliant quick-and-easy way to do it; using JavaScript is an option.

The real question is, why do you want users to not be able to highlight and presumably copy and paste certain elements? I have not come across a single time that I wanted to not let users highlight a certain portion of my website. Several of my friends, after spending many hours reading and writing code will use the highlight feature as a way to remember where on the page they were, or providing a marker so that their eyes know where to look next.

The only place I could see this being useful is if you have buttons for forms that should not be copy and pasted if a user copy and pasted the website.

Darlinedarling answered 5/5, 2009 at 20:37 Comment(3)
This may be necessary for embedded devices. i.e. a device where a browser is used for rendering the UI.Booker
Another reason this is needed is Shift-clicking to select multiple rows in a grid or table. You don't want to to highlight the text, you want it to select the rows.Soapwort
Highly interactive web app with a lot of drag & drop... accidental highlighting is a big usability problem.Hanson
S
209

A JavaScript solution for Internet Explorer is:

onselectstart="return false;"
Sapers answered 13/11, 2009 at 16:5 Comment(1)
Don’t forget about ondragstart!Riba
L
158

If you want to disable text selection on everything except on <p> elements, you can do this in CSS (watch out for the -moz-none which allows override in sub-elements, which is allowed in other browsers with none):

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}

p {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
}
Layoff answered 24/5, 2011 at 21:24 Comment(5)
Make sure you also make input fields selectable: p, input { -webkit-user-select: text; -khtml-user-select: text; -moz-user-select: text; -o-user-select: text; user-select: text; }Glyconeogenesis
Be very wary about turning off browser UI expectations on ALL code except for one item. What about list items <li /> text, for example?Centrobaric
Just an update... according to MDN since Firefox 21 -moz-none and none are the same.Finochio
For this you may add cursor:default and cursor:text respectively : )Cusack
THE bomb. That is to say. THE END. ul>* { -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: -moz-none; -o-user-select: none; user-select: none; } [selects everything in an unordered list, and makes it un-selectable, rather than trashing the whole view tree.] Thanks for the lesson. My button list is looking great, and responding correctly to screen tapping and pressing, rather than launching an IME (android clipboard widgets).Caoutchouc
I
140

In the solutions in previous answers selection is stopped, but the user still thinks you can select text because the cursor still changes. To keep it static, you'll have to set your CSS cursor:

.noselect {
    cursor: default;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
<p>
  Selectable text.
</p>
<p class="noselect">
  Unselectable text.
</p>

This will make your text totally flat, like it would be in a desktop application.

Industrious answered 30/8, 2015 at 18:32 Comment(3)
"Flat" as opposed to what?Riot
@Riot As opposed to "layered". Instead of text floating on top of the other elements. It is similar to the difference between SVG and PNG images.Cottonweed
Was surprised to discover that Firefox still requires the vendor prefix in 2019. I disregardfully used only user-select: none;, thinking the standard would be adopted by now, but sadly it has not. Makes you wonder what the people on the standards committee could still be debating. "No, you guys... I really think it should be user-select: cant; because it's like more descriptive, you know?" "We've been over this, Mike. We would have to omit the apostrophe, and that's bad form!" "Enough, everyone! We will deliberate on this matter again next month. Standards Committee meeting adjourned!"Dichogamy
F
122

You can do so in Firefox and Safari (Chrome also?)

::selection { background: transparent; }
::-moz-selection { background: transparent; }
Foppery answered 5/5, 2009 at 20:46 Comment(2)
I wouldn't recommend doing this, because it doesn't actually fix the issue; disabling text selection - it merely hides it. This can lead to bad usability, because if I drag my cursor around the page I could be selecting any arbitrary text without knowing it. This can cause all kinds of weird usability "bugs".Babita
Doesn't work on PNG-images with transparent areas: The will always select in a light blue… Any workaround?Oubliette
H
91

Workaround for WebKit:

/* Disable tap highlighting */
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);

I found it in a CardFlip example.

Harm answered 21/9, 2011 at 7:9 Comment(1)
Using transparent in lieu of rgba also works in Chrome 42 on Android.Walterwalters
H
88

I like the hybrid CSS + jQuery solution.

To make all elements inside <div class="draggable"></div> unselectable, use this CSS:

.draggable {
    -webkit-user-select: none;
     -khtml-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
         -o-user-select: none;
            user-select: none;
}

.draggable input {
    -webkit-user-select: text;
     -khtml-user-select: text;
       -moz-user-select: text;
         -o-user-select: text;
            user-select: text;
 }

And then, if you're using jQuery, add this inside a $(document).ready() block:

if (($.browser.msie && $.browser.version < 10) || $.browser.opera) $('.draggable').find(':not(input)').attr('unselectable', 'on');

I figure you still want any input elements to be interactable, hence the :not() pseudo-selector. You could use '*' instead if you don't care.

Caveat: Internet Explorer 9 may not need this extra jQuery piece, so you may want to add a version check in there.

Hydrogenous answered 11/11, 2011 at 19:53 Comment(7)
Use -ms-user-select: none; (for IE10) and your jQuery "if" should be this: if (($.browser.msie && $.browser.version < 10) || $.browser.opera)Toughminded
Be careful man !!! To make it selectable in firefox you must use -moz-user-select: Normal;Christoforo
@Toughminded jQuery.browser has been deprecated as of version 1.3 and has been removed in version 1.9 - api.jquery.com/jQuery.browserGalleon
@Wynand Good point. But what sort of "feature detection" exists to determine which CSS property to use?Hydrogenous
@TomAuger You could use jQuery.support, it allows you to check for single features : LinkIndustry
Yeah, but what exactly are you going to query in terms of support? I wasn't aware that you could query support for a particular CSS property...Hydrogenous
@PeterMortensen but user-select: none doesnt even work on input elements?!?! you can always still select the text. how do i make user-select: none work on input elements????Garbage
D
81

You can use CSS or JavaScript for that.

The JavaScript way is supported in older browsers, like old versions of Internet Explorer as well, but if it's not your case, use the CSS way then:

HTML/JavaScript:

<html onselectstart='return false;'>
  <body>
    <h1>This is the Heading!</h1>
    <p>And I'm the text, I won't be selected if you select me.</p>
  </body>
</html>

HTML/CSS:

.not-selectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<body class="not-selectable">
  <h1>This is the Heading!</h1>
  <p>And I'm the text, I won't be selected if you select me.</p>
</body>
Disciplinary answered 5/6, 2017 at 14:3 Comment(0)
S
78

.hidden:after {
    content: attr(data-txt);
}
<p class="hidden" data-txt="Some text you don't want to be selected"></p>

It's not the best way, though.

Sansculotte answered 1/5, 2013 at 11:36 Comment(5)
You could also use title as the attribute.Juridical
That is a very creative solution. Especially if it used the title attribute because that would probably be better for screen readers.Novel
I tried it (JSBin) and it doesn't work in IE. Unfortunately older IEs are the only ones that user-select doesn't work for.Novel
This is a great non-JS alternative that works in Chrome! Awesome!Trihydric
This was what I needed to prevent actual selection rather than just preventing the display of selection.Lineation
F
67

For Internet Explorer in addition, you need to add pseudo class focus (.ClassName:focus) and outline-style: none.

.ClassName,
.ClassName:focus {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    outline-style: none; /* Internet Explorer  */
}
Fable answered 23/12, 2013 at 14:5 Comment(1)
This does work in IE so long as the selection starts on an element with the className class. See this JSBin.Novel
P
63

Try to insert these rows into the CSS and call the "disHighlight" at class property:

.disHighlight {
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -o-user-select: none;
    -moz-user-select: none;
}
Plumbaginaceous answered 28/8, 2016 at 7:13 Comment(0)
M
62

A Quick Hack Update

If you use the value none for all the CSS user-select properties (including browser prefixes of it), there is a problem which can be still occurred by this.

.div {
    -webkit-user-select: none; /* Chrome all / Safari all */
    -moz-user-select: none;    /* Firefox all             */
    -ms-user-select: none;     /* Internet Explorer  10+  */
     user-select: none;        /* Likely future           */
}

As CSS-Tricks says, the problem is:

WebKit still allows the text to be copied, if you select elements around it.

You can also use the below one to enforce that an entire element gets selected which means if you click on an element, all the text wrapped in that element will get selected. For this all you have to do is changing the value none to all.

.force-select {
    -webkit-user-select: all;  /* Chrome 49+     */
    -moz-user-select: all;     /* Firefox 43+    */
    -ms-user-select: all;      /* No support yet */
    user-select: all;          /* Likely future  */
}
Magnetostriction answered 31/3, 2018 at 11:32 Comment(0)
W
56

With SASS (SCSS syntax)

You can do this with a mixin:

// Disable selection
@mixin disable-selection {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none;   /* Safari */
    -khtml-user-select: none;    /* Konqueror HTML */
    -moz-user-select: none;      /* Firefox */
    -ms-user-select: none;       /* Internet Explorer/Edge */
    user-select: none;           /* Non-prefixed version, currently supported by Chrome and Opera */
}

// No selectable element
.no-selectable {
    @include disable-selection;
}

In an HTML tag:

<div class="no-selectable">TRY TO HIGHLIGHT. YOU CANNOT!</div>

Try it in this CodePen.

If you are using an autoprefixer you can remove other prefixes.

Browser compatibility here.

Weisman answered 11/10, 2019 at 10:27 Comment(0)
L
53

For those who have trouble achieving the same in the Android browser with the touch event, use:

html, body {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-tap-highlight-color: transparent;
}
Lanam answered 19/5, 2014 at 5:30 Comment(0)
V
50

If you are using Less and Bootstrap you could write:

.user-select(none);
Venenose answered 18/4, 2012 at 8:34 Comment(0)
T
50
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   user-select: none;
}
<div id="foo" unselectable="on" class="unselectable">...</div>
function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.unselectable = true;
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));
-webkit-user-select: none;
-moz-user-select: none;
onselectstart="return false;"
::selection { 
    background: transparent; 
}

::-moz-selection { 
    background: transparent; 
}

* {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -o-user-select: none;
    user-select: none;
}

p {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
}
<div class="draggable"></div>
.draggable {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
}

.draggable input {
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -o-user-select: text;
    user-select: text;
 }
if ($.browser.msie)
    $('.draggable').find(':not(input)').attr('unselectable', 'on');
Thermostatics answered 26/10, 2012 at 5:44 Comment(0)
S
39

Aside from the Mozilla-only property, no, there is no way to disable text selection with just standard CSS (as of now).

If you notice, Stack Overflow doesn't disable text selection for their navigation buttons, and I would recommend against doing so in most cases, since it modifies normal selection behavior and makes it conflict with a user's expectations.

Sarson answered 5/5, 2009 at 20:38 Comment(7)
While I agree that it changes behaviour the user expects, it would make sense for things like the "Add Comment" button that is sitting next to this form field ...Darlinedarling
But doesn't that expose needless implementation details? An input or button's text can't be selected.Visor
@anon: Most users will probably not try to select the text of your button, so in practice, it shouldn't really matter much. Besides, in order to do so, they will have to start selecting outside of the button—if they click inside the button itself, the onclick handler will activate instead. Plus, certain browsers (e.g. Safari) actually let you select the text of normal buttons…Sarson
If you're selecting a set of comments from a chat thread and each comment has an upvote/downvote button next to it, then it would be nice to select the text without the other stuff. That's what the user expects or wants. He doesn't want to copy/paste the button labels with every comment.Monoceros
And what if you for example double click a button which instead of redirecting you to another page opens a div? then the text for the button will be selected due to the double-click!Dandrea
Make the things you want selectable - selectable... and nothing else.Rattigan
Selecting records in a table is another exception. If you want to enable holding shift/control to select and highlight multiple records, then you have to disable text-selection.Alburnum
V
38

This works in some browsers:

::selection{ background-color: transparent;}
::moz-selection{ background-color: transparent;}
::webkit-selection{ background-color: transparent;}

Simply add your desired elements/ids in front of the selectors separated by commas without spaces, like so:

h1::selection,h2::selection,h3::selection,p::selection{ background-color: transparent;}
h1::moz-selection,h2::moz-selection,h3::moz-selection,p::moz-selection{ background-color: transparent;}
h1::webkit-selection,h2::webkit-selection,h3::webkit-selection,p::webkit-selection{ background-color: transparent;}

The other answers are better; this should probably be seen as a last resort/catchall.

Valois answered 9/4, 2014 at 22:56 Comment(1)
There are few things that can be known for sure, but this solution definitely doesn't work in all browsers.Kongo
D
37

Suppose there are two divs like this:

.second {
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  -webkit-touch-callout: none;
  /* iOS Safari */
}
<div class="first">
  This is my first div
</div>

<div class="second">
  This is my second div
</div>

Set cursor to default so that it will give a unselectable feel to the user.

Prefix need to be used to support it in all browsers. Without a prefix this may not work in all the answers.

Diannediannne answered 28/3, 2016 at 9:42 Comment(0)
E
33

This will be useful if color selection is also not needed:

::-moz-selection { background:none; color:none; }
::selection { background:none; color:none; }

...all other browser fixes. It will work in Internet Explorer 9 or later.

Exactly answered 3/4, 2013 at 8:31 Comment(2)
Make that color: inherit; maybe.Ypres
yeah I love it. It's css selector level 3 according to Mozilla docsOgbomosho
F
32

Add this to the first div in which you want to disable the selection for text:

onmousedown='return false;' 
onselectstart='return false;'
Floyd answered 30/10, 2012 at 6:56 Comment(0)
E
30

NOTE:

The correct answer is correct in that it prevents you from being able to select the text. However, it does not prevent you from being able to copy the text, as I'll show with the next couple of screenshots (as of 7th Nov 2014).

Before we have selected anything

After we have selected

The numbers have been copied

As you can see, we were unable to select the numbers, but we were able to copy them.

Tested on: Ubuntu, Google Chrome 38.0.2125.111.

Elaterin answered 7/11, 2014 at 13:22 Comment(1)
I've had the same problem. On Mac Chrome 48.0.2564.116 and on Mac Safari 9.0.3. Notably, Mac Firefox 43.0 doesn't copy the character, but sticks extra endlines between them. What should be done about this?Egis
A
28

It is easily done with:

-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;

Alternatively:

Let's say you have a <h1 id="example">Hello, World!</h1>. You will have to remove the innerHTML of that h1, in this case Hello, World. Then you will have to go to CSS and do this:

#example::before // You can of course use **::after** as well.
{
    content: 'Hello, World!'; // Both single-quotes and double-quotes can be used here.

    display: block; // To make sure it works fine in every browser.
}

Now it simply thinks it is a block-element, and not text.

Afore answered 8/11, 2018 at 9:17 Comment(0)
T
27

To get the result I needed, I found I had to use both ::selection and user-select

input.no-select:focus {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

input.no-select::selection {
    background: transparent;
}

input.no-select::-moz-selection {
    background: transparent;
}
Thicket answered 14/5, 2015 at 0:13 Comment(0)
P
23

This is not CSS, but it is worth a mention:

jQuery UI Disable Selection:

$("your.selector").disableSelection();
Platina answered 9/4, 2013 at 16:42 Comment(0)
M
21

FIRST METHOD: ( TOTALLY NONSENSE ):

.no-select::selection, .no-select *::selection {
  background-color: Transparent;
}

.no-select { /* Sometimes I add this too. */
  cursor: default;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

Snippet:

.no-select::selection, .no-select *::selection {
  background-color: Transparent;
}

.no-select {
  /* Sometimes I add this too. */
  cursor: default;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

SECOND METHOD ( NO NONSENSE INCLUDED )

.no-select {
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
}

Snippet:

.no-select {
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
}
<span>RixTheTyrunt is da best!</span>
<br>
<span class="no-select">RixTheTyrunt is da best!</span>

First, solve the problem. Then, write the code.

John Johnson

Munafo answered 5/5, 2009 at 20:29 Comment(0)
P
21

Check my solution without JavaScript:

jsFiddle

li:hover {
    background-color: silver;
}
#id1:before {
    content: "File";
}
#id2:before {
    content: "Edit";
}
#id3:before {
    content: "View";
}
<ul>
    <li><a id="id1" href="www.w1.com"></a>
    <li><a id="id2" href="www.w2.com"></a>
    <li><a id="id3" href="www.w3.com"></a>
</ul>

Popup menu with my technique applied: http://jsfiddle.net/y4Lac/2/

Primp answered 22/2, 2014 at 22:6 Comment(0)
F
21

I have learned from the CSS-Tricks website.

user-select: none;

And this also:

::selection {
    background-color: transparent;
}

::moz-selection {
    background-color: transparent;
}

::webkit-selection {
    background-color: transparent;
}
Fluster answered 1/4, 2016 at 11:16 Comment(1)
It only makes it invisibleCommutable
B
20

Though this pseudo-element was in drafts of CSS Selectors Level 3, it was removed during the Candidate Recommendation phase, as it appeared that its behavior was under-specified, especially with nested elements, and interoperability wasn't achieved.

It's being discussed in How ::selection works on nested elements.

Despite it is being implemented in browsers, you can make an illusion of text not being selected by using the same color and background color on selection as of the tab design (in your case).

Normal CSS Design

p { color: white;  background: black; }

On selection

p::-moz-selection { color: white;  background: black; }
p::selection      { color: white;  background: black; }

Disallowing users to select the text will raise usability issues.

Bordereau answered 21/3, 2014 at 13:53 Comment(1)
This must be why Netbeans auto-completion has no idea what I am talking about!Countermand
D
19

If you want to disable selection and highlighting for the whole page with CSS, you can easily apply it to all elements:

* {
    -webkit-touch-callout: none; /* iOS Safari */
      -webkit-user-select: none; /* Safari */
       -khtml-user-select: none; /* Konqueror HTML */
         -moz-user-select: none; /* Firefox */
          -ms-user-select: none; /* Internet Explorer/Edge */
              user-select: none; /* Non-prefixed version, currently
                                    supported by Chrome and Opera */
}
Dupleix answered 30/4, 2018 at 3:47 Comment(0)
Z
15

Have you tried this?

.disableSelect{
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -o-user-select: none;
    -moz-user-select: none;

    pointer-events:none;
}
Zama answered 3/9, 2020 at 14:14 Comment(2)
This is required to disable text input selection. user-select: none must be at div class and pointer-events: none at input classHerb
pointer-events was the secret I needed, to stop a not-technically-background image from being selectedAimless
A
11

You can use a *-user-select property as below for that...

p {
    -webkit-user-select: none;  /* Chrome all and Safari all */
    -moz-user-select: none;     /* Firefox all */
    -ms-user-select: none;      /* Internet Explorer 10 and later */
    user-select: none;          /* Likely future */
}

Link for the Detailed Description

Adipose answered 6/9, 2016 at 9:4 Comment(0)
P
10

Try to use this one:

::selection {
    background: transparent;
}

And if you wish to specify not select inside a specific element, just put the element class or id before the selection rule, such as:

.ClassNAME::selection {
    background: transparent;
}
#IdNAME::selection {
    background: transparent;
}
Para answered 20/1, 2016 at 9:22 Comment(1)
That does not disable select at all...It only makes it invisibleCommutable
S
9

Add a class to your CSS that defines you cannot select or highlight an element. I have an example:

<style> 
    .no_highlighting{
        user-select: none;
    }

    .anchor_without_decoration:hover{
        text-decoration-style: none;
    }
</style>

<a href="#" class="anchor_without_decoration no_highlighting">Anchor text</a>
Soliloquy answered 6/3, 2020 at 10:33 Comment(0)
H
8

This highlighting effect is due to the action called hover (onMouseHover).

When you will hover on any tab its color will be changed.

Just say for example,

HTML code

<div class="menu">
    <ul class="effect">
        <li>Questions</li>
        <li>JOBS</li>
        <li>Users</li>
    </ul>
</div>

CSS code

.effect:hover {
    color: none;
}

You can use any color if you want to get it highlighted. Else you can use none.

Hildie answered 3/6, 2016 at 13:12 Comment(1)
No, what you are talking about is hovering, which means "when the mouse is over the text". It is different from "selecting the text". By default there is no color when hovering.Foreignborn
P
8

With CSS-

div {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -o-user-select: none;

  "unselectable='on' onselectstart="return false;"
  onmousedown="return false;
}
<div>
  Blabla
  <br/> More Blabla
  <br/> More Blabla...
</div>
Passover answered 22/9, 2018 at 14:23 Comment(0)
P
6

I see many detailed answers but I believe that writing just this line of code should be enough for the required task:

*{
    -webkit-user-select: none;
 }
Protuberance answered 10/7, 2021 at 11:16 Comment(0)
O
5

Even better, you can disable text selection.

If you like Sass (SCSS), and you don't want to use Compass you can do this:

styles.scss

@mixin user-select($select) {
    -webkit-touch-callout:#{$select};
    @each $pre in -webkit-, -moz-, -ms-, -o-, -khtml- {
        #{$pre + user-select}: #{$select};
    }
    #{user-select}: #{$select};
}

.no-select {
    @include user-select(none);
}
Okinawa answered 31/1, 2018 at 0:5 Comment(0)
F
4

I combined the various browser CSS select attributes with the unselectable tag required for Internet Explorer < 9.

<style>
[unselectable="on"] {
    -webkit-user-select: none; /* Safari */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* Internet Explorer 10+/Edge */
    user-select: none; /* Standard */
}
</style>
<div unselectable="on">Unselectable Text</div>
Faircloth answered 1/2, 2018 at 20:23 Comment(0)
F
1
::selection {background: transparent; color: transparent;}

::-moz-selection {background: transparent; color: transparent;}
Fireworm answered 31/10, 2018 at 6:13 Comment(3)
Can you explain further what you mean by this? How does this improve the other highly upvoted answers?Sucrose
This does not disable the text-selection, all it does is hide it for the user. The computer itself still knows it is being selected, the background-color is just transparent. But if you press ctr (cmd on mac) + A and then copy you can paste the text somewhere else.Afore
An explanation would be in order.Wandawander
B
0

Maybe you can use this solution through :before:

nav li {
    display: inline-block;
    position: relative;
}

nav li a {
    display: inline-block;
    padding: 10px 20px;
}

nav li a:hover {
    color: red;
}

nav li.disabled:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}
<nav>
    <ul>
    <li><a href="#">Link</a></li>
    <li class="disabled"><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
    </ul>
</nav>

JsFiddle - https://jsfiddle.net/grinmax_/9L1cckxu/

Booklet answered 2/3, 2017 at 17:34 Comment(1)
This is not an answer, you can still select the text.Afore
L
0

You may also want to prevent the context menu appearing when touching elements like buttons that have their selection prevented. To do that, add this code to the entire page, or just those button elements:

$("body").on("contextmenu",function(e){
    return false;
});
Livery answered 27/8, 2019 at 7:17 Comment(0)
B
-2

This may work

    ::selection {
        color: none;
        background: none;
    }
    /* For Mozilla Firefox */
    ::-moz-selection {
        color: none;
        background: none;
    }
Bodoni answered 17/1, 2020 at 1:1 Comment(1)
When adding answers to ten year old questions with forty four other answers the bar is higher. It is useful to address in your answer what new aspect of the question your answer addresses and how the passage of time an emergence of new technologies has impacted the answer. The answer should also explain how and what the code does.Confidant

© 2022 - 2024 — McMap. All rights reserved.